package com.yjq.programmer;

import com.yjq.programmer.domain.ParamProperties;
import com.yjq.programmer.utils.FreemarkerUtil;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author 杨杨吖
 * @create 2023-10-27 16:26
 */
public class GeneratorApplication {

    static String consumerPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\consumer\\";
    static String controllerPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\controller\\";
    static String providerPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\provider\\";
    static String protoPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\proto\\";
    static String modelPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\model\\";
    static String daoPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\dao\\";
    static String mapperPath = System.getProperty("user.dir") + "\\generator-freemarker\\src\\main\\java\\com\\yjq\\programmer\\generator\\mapper\\";

    public static void main( String[] args ) throws Exception {
        Map<String, Object> map = new HashMap<>();
        Map<String, ParamProperties> paramsMap = new LinkedHashMap<>();
        Map<String, ParamProperties> responseParamsMap = new LinkedHashMap<>();



        // 写入参
        // 字段名称：单个单词全小写，多个单词用_隔开 第二个单词开始首字母大写 如：code org_Flag  task_Child_Type
        // 上面字段名称写法要求主要是针对provider里面入参展示为I_ORG_FLAG I_TASK_CHILD_TYPE这样，如果没这要求，入参写成小驼峰 orgFlag taskChildType也行
        // 小驼峰形式的入参 在provider里面入参展示为I_ORGFLAG I_TASKCHILDTYPE这样
        // 字段类型fieldType：string | int

        paramsMap.put("cust_Id", new ParamProperties("客户id", "string"));
        paramsMap.put("source", new ParamProperties("来源表", "string"));


        // 写出参 -- responseType为response时候才需要写 只针对查询
        // 字段名称：小驼峰形式  orgFlag、name、kunKunJi
        // 字段类型fieldType：string | int
        responseParamsMap.put("custId", new ParamProperties("客户id", "string"));
        responseParamsMap.put("market", new ParamProperties("市场id", "string"));
        responseParamsMap.put("accountId", new ParamProperties("证券账号", "string"));
        responseParamsMap.put("seatCode", new ParamProperties("席位号", "string"));





        // 写接口名
        map.put("service", "queryCustomerAccount");
        // 设置返回格式 JSON字符串(json_string) | Response(response)
        // 只针对查询
        map.put("responseType", "response");
        // 写接口注释
        map.put("desc", "查询结算文件账户列表");
        // 写操作类型 query | insert | update | delete
        map.put("operateType", "query");
        // 是否分页  是|否
        map.put("paging", "否");





        map.put("paramsMap", paramsMap);
        map.put("responseParamsMap", responseParamsMap);
        // 生成consumer
        generatorConsumer(map);
        // 生成controller
        generateController(map);
        // 生成provider
        generateProvider(map);
        // 生成proto
        generateProto(map);
        // 生成model
        generateModel(map);
        // 生成dao
        generateDao(map);
        // 生成mapper
        generateMapper(map);

        System.out.println("生成完成！");

    }

    public static void generatorConsumer(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/consumer.ftl");
        FreemarkerUtil.generator(consumerPath + "ConsumerGenerator.txt", map);
    }

    public static void generateController(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/controller.ftl");
        FreemarkerUtil.generator(controllerPath + "ControllerGenerator.txt", map);
    }

    public static void generateProvider(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/provider.ftl");
        FreemarkerUtil.generator(providerPath + "ProviderGenerator.txt", map);
    }

    public static void generateProto(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/proto.ftl");
        FreemarkerUtil.generator(protoPath + "ProtoGenerator.txt", map);
    }

    public static void generateModel(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/model.ftl");
        String service = (String) map.get("service");
        FreemarkerUtil.generator(modelPath + service.substring(0,1).toUpperCase() + service.substring(1) + "Model.java", map);
    }

    public static void generateDao(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/dao.ftl");
        FreemarkerUtil.generator(daoPath + "DaoGenerator.txt", map);
    }

    public static void generateMapper(Map<String, Object> map) throws Exception {
        FreemarkerUtil.initConfig(judgeOperateType(map) + "/mapper.ftl");
        FreemarkerUtil.generator(mapperPath + "MapperGenerator.txt", map);
    }

    public static String judgeOperateType(Map<String, Object> map) {
        Optional.ofNullable(map.get("operateType")).orElseThrow(() -> new RuntimeException("操作类型不能为空！"));
        String operateType = (String) map.get("operateType");
        switch (operateType) {
            case "query":
                return "query";
            case "insert":
            case "update":
            case "delete":
                return "operate";
            default:
                throw new RuntimeException("操作类型有误");
        }
    }
}
